using System;
using System.Collections.Generic;
using System.Text;

using System.Runtime.InteropServices;


namespace MediaBridge
{
    public class MediaBridgeGraphInfo
    {
        private string _mediaUrl = "";
        
        private IntPtr _pFilterGraph;

        public string MediaUrl
        {
            get{return _mediaUrl;}
            set{_mediaUrl = value;}
        }

        public IntPtr FilterGraph
        {
            get { return _pFilterGraph; }
            set{_pFilterGraph = value;}
        }

    }

    public class MediaBridgeManager
    {
        public delegate void NewMediaGraphInfo(MediaBridgeGraphInfo GraphInfo);

        private static Dictionary<string, NewMediaGraphInfo> _delegateHash = new Dictionary<string, NewMediaGraphInfo>();

       

        static MediaBridgeManager()
        {
           
           
        }

        private static string FormatUrl(string Url)
        {
            Url = Url.ToLower();
            Url += "/";

            return Url;
        }

        public static void AddMediaGraphInfo(MediaBridgeGraphInfo GraphInfo)
        {
            if (GraphInfo != null)
            {
                if (_delegateHash.ContainsKey(GraphInfo.MediaUrl))
                {
                    NewMediaGraphInfo callback = _delegateHash[GraphInfo.MediaUrl];
                    _delegateHash.Remove(GraphInfo.MediaUrl);
                    callback(GraphInfo);
                }
            }
        }

        public static void UnregisterCallback(string MediaUrl)
        {
            MediaUrl = FormatUrl(MediaUrl);
            if (_delegateHash.ContainsKey(MediaUrl))
            {
                _delegateHash.Remove(MediaUrl);
            }
        }

        public static bool RegisterCallback(string MediaUrl, NewMediaGraphInfo Callback)
        {
            MediaUrl = FormatUrl(MediaUrl);
            if (_delegateHash.ContainsKey(MediaUrl)) return false;
            
            _delegateHash.Add(MediaUrl, Callback);
            return true;
        }
    }
}
